/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
/*
  // 层序遍历统计  O(n)
    int countNodes(TreeNode* root) {
        queue<TreeNode*>q;
        if(nullptr != root) q.push(root);
        size_t nodenum = 0;
        while(!q.empty()){
            size_t size = q.size();
            for(size_t i = 0; i < size; ++i){
                TreeNode* node = q.front();
                ++nodenum;
                q.pop();
                if(node->left) q.push(node->left);
                if(node->right) q.push(node->right);
            }
        }
        return nodenum;
    }
*/


/*  满二叉树：用 2^树深度 - 1 来计算，这里根节点（只有一个节点的满二叉树）深度为1  */
    int countNodes(TreeNode* root) {
        if(nullptr == root) return 0;
        TreeNode* left = root->left, *right = root->right;
        int leftnum = 0, rightnum = 0;
        /*
        while(left){
            left = left->left;
            ++leftnum;
        }
        while(right){
            right = right->right;
            ++rightnum;
        }

        if(leftnum == rightnum)
            return (2 << leftnum) - 1;    // 2^深度 - 1；
        */
        return countNodes(root->left)+countNodes(root->right)+1;
    }
};
